package com.mizhiquan.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.jaq.model.v20161123.AfsCheckRequest;
import com.aliyuncs.jaq.model.v20161123.AfsCheckResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.mizhiquan.common.Const;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 阿里云滑动验证工具
 * Created by Shane on 2017/7/11.
 */
public class VerificationUtil {

    public static boolean verify(String sid,String sig,String token,String scene) {
        if (sid == null || sig == null || token == null || scene == null || sid == "" || sig == "" || token == "" || scene == "")
            return false;
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", Const.accessKeyId,Const.accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "jaq", "jaq.aliyuncs.com");
        } catch (ClientException e1) {
            e1.printStackTrace();
        }
        AfsCheckRequest arequest = new AfsCheckRequest();
        arequest.setPlatform(3);//必填参数，请求来源： 1：Android端； 2：iOS端； 3：PC端及其他
        arequest.setSession(sid);// 必填参数，从前端获取，不可更改
        arequest.setSig(sig);// 必填参数，从前端获取，不可更改
        arequest.setToken(token);// 必填参数，从前端获取，不可更改
        arequest.setScene(scene);// 必填参数，从前端获取，不可更改

        AfsCheckResponse aresponse = null;
        try {
            aresponse = client.getAcsResponse(arequest);
            if (aresponse.getData() != true) {
                return false;
            }else{
                return true;
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;

    }
}
